#pragma once

#include <QWidget>
#include <QVBoxLayout>
#include <QTableView>
#include <QStandardItemModel>
#include <QElapsedTimer>
#include <QButtonGroup>
#include <map>
#include "core/system/scoped_refptr.h"
#include "qbmessage/include/message_bond.h"
//#include "ui_BestQuoteWidget.h"
#include "BestQuote/BestQuoteData.h"
#include "BestQuote/BestQuoteMiniFilter.h"

using namespace qb;

class QTimer;
class ItemFlashModel
{
public:
    ItemFlashModel(QTableView* t);
    ~ItemFlashModel();
	void addFlashData(BondIndexBroker bi, bool end = false);
    float flashPercent(qint64) const;
    void update();

private:
	std::map<qint64, int>   m_flashData;
    QTimer* m_timer = nullptr;
    QTableView* m_table = nullptr;
    QElapsedTimer   m_countTimer;
};

class BestQuoteModel : public QStandardItemModel
{
public:
    BestQuoteModel() {}
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    void setFlashModel(ItemFlashModel* flash) {
        m_flashModel = flash;
    }
private:
    ItemFlashModel* m_flashModel = nullptr;
};

class BestQuoteProcess;
class BestQuoteWidget
    : public QWidget
{
    Q_OBJECT

public:
    BestQuoteWidget(const QString& title, QWidget* parent = nullptr);
    ~BestQuoteWidget();

public slots:
    void OnTabBrokerClick(int id);
	void OnClickHeader(int col);
	void OnTableViewMouseWheel(int delta);
	void OnScrollBarChange(int position);
    void OnDataProcessFinish(void* pData);
    void OnFreeAckData(void* pData);
    void OnDestroy();

protected:
    void initCtrl();
    void initData();
    void createTabBroker();
    void createQuoteView();
	void updateView();
    void updateData(void* pData);
    void resetScrollBar(bool bReset = false); //bReset==true时，scrollbar重置到第一行

private:
//    Ui_BestQuoteWidget		m_ui;

//    QHBoxLayout*                            m_mainLayout;
    QButtonGroup*                           m_tabBroker;
    BestQuoteModel*                         m_bestquoteItemModel;
    QTableView*                             m_tableviewBestQuote;
    QScrollBar*                             m_vertScrollBar;

    int                                     m_pushIdx;
    int                                     m_maxRowCount;

    std::map<std::string, int>*             m_pIndexMap;

    BestQuoteProcess*                         m_process;
    ItemFlashModel* m_flash = nullptr;
};
